﻿Public Class ViewType

    Private Sub ViewType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        loaddata()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm = New Type
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New Type
        frm.l.Text = row.Cells("TypeId").Value
        frm.txttype.Text = row.Cells("TypeName").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub
    Private Sub loaddata()
        cmd.Parameters("@S") = txtSearch.Text
        DataGridView1.DataSource = cmd.ExecuteDataTable("SELECT * FROM Type WHERE Active=1 AND UPPER(TypeName) LIKE UPPER('%'+@S+'%');")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim id = row.Cells("TypeId").Value

        cmd.Parameters("@Id") = id
        cmd.ExecuteNonQuery("UPDATE [Type] SET Active=0 WHERE TypeId=@Id")
        loaddata()
    End Sub
End Class